var ProfileUpdate = function () {

    var initPickers = function () {
        
        jQuery.validator.addMethod("regex", function (value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Giá trị không hợp lệ"
        );
    
        //init date pickers
        $('.date-picker').datepicker({
            rtl: Metronic.isRTL(),
            language: 'vi',
            autoclose: true
        });
    }
 
    var handleValidation_Profile = function() {
 
        var form = $('#form-profile');
        var error = $('.alert-danger', form);
        var success = $('.alert-success', form);
 
        form.validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                username: {
                    minlength: 10,
                    required: true
                },
                birthday: {
                    regex: /(\d{2})-(\d{2})-(\d{4})/,
                    required: true
                },
                baptism_date: {
                    regex: /[0-9][0-9]-[0-1][0-9]-[1-2][0-9][0-9][0-9]/
                },
                eucharist_date: {
                    regex: /[0-9][0-9]-[0-1][0-9]-[1-2][0-9][0-9][0-9]/
                },
                confirmation_date: {
                    regex: /[0-9][0-9]-[0-1][0-9]-[1-2][0-9][0-9][0-9]/
                },
                email: {
                    email: true
                },
                password: {
                    minlength: 6,
                    regex: /(?=.*\d)(?=.*[a-zA-Z]).{6,}/,
                    required: true
                },
                retype_password: {
                    minlength: 6,
                    required: true,
                    equalTo : "#password"
                }
            },
            messages: {
                username: {
                    minlength: 'Tối thiểu 10 ký tự',
                    required: 'Bắt buộc nhập'
                },
                email: {
                    email: 'Email không hợp lệ'
                },
                password: {
                    minlength: 'Tối thiểu 6 ký tự',
                    required: 'Bắt buộc nhập',
                    regex: "Mật Khẩu phải chứa ký tự số <strong>0-9</strong> và chữ cái <strong>a-Z</strong>"
                },
                retype_password: {
                    minlength: 'Tối thiểu 6 ký tự',
                    required: 'Bắt buộc nhập',
                    equalTo : "Chưa khớp với Mât Khẩu Mới"
                }
            },
 
            invalidHandler: function (event, validator) { //display error alert on form submit 
                success.hide();
                error.show();
                Metronic.scrollTo(error, -200);
            },
 
            errorPlacement: function (error, element) { // render error placement for each input type
                if (element.parent(".input-group").size() > 0) {
                    error.insertAfter(element.parent(".input-group"));
                } else if (element.attr("data-error-container")) { 
                    error.appendTo(element.attr("data-error-container"));
                } else if (element.parents('.radio-list').size() > 0) { 
                    error.appendTo(element.parents('.radio-list').attr("data-error-container"));
                } else if (element.parents('.radio-inline').size() > 0) { 
                    error.appendTo(element.parents('.radio-inline').attr("data-error-container"));
                } else if (element.parents('.checkbox-list').size() > 0) {
                    error.appendTo(element.parents('.checkbox-list').attr("data-error-container"));
                } else if (element.parents('.checkbox-inline').size() > 0) { 
                    error.appendTo(element.parents('.checkbox-inline').attr("data-error-container"));
                } else {
                    error.insertAfter(element); // for other inputs, just perform default behavior
                }
            },
 
            highlight: function (element) { // hightlight error inputs
                $(element)
                .closest('.form-group').addClass('has-error'); // set error class to the control group 
            },
 
            unhighlight: function (element) { // revert the change done by hightlight
 
            },
 
            success: function (label, element) {
                var icon = $(element).parent('.input-icon').children('i');
                $(element).closest('.form-group').removeClass('has-error'); // set success class to the control group
                icon.removeClass("fa-warning");
            },
 
            submitHandler: function (form) {
                success.hide();
                error.hide();

                form = $(form);
                
                $.ajax({
                    url: form.attr('action'),
                    type: form.attr('method'),
                    data: form.serialize(),
                    dataType: 'json'
                }).done(function(response) {
                    if (response.code == 200) {
                        success.find('span').text(response.message);
                        success.show();
                        error.hide();
                    }
                    else {
                        error.find('span').text(response.message);
                        error.show();
                        success.hide();
                    }
                })
                .fail(function() {
                    error.find('span').text('Check Your Connection.');
                    error.show();
                    success.hide();
                });
                
                return false;
            }
        });
    }
     
    return {
        //main function to initiate the module
        init: function () {
 
            initPickers();
            handleValidation_Profile();
        }
    };

}();